home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / intrlib1.zip / PD_MENUS.C < prev    next >
C/C++ Source or Header  |  1992-01-20  |  9KB  |  241 lines

  1. /******************************************************************************
  2. * Iteraction library - pull down menus handler.                      *
  3. *                                          *
  4. *                    Written by Gershon Elber,  Oct. 1990  *
  5. *******************************************************************************
  6. * History:                                      *
  7. *  3 Oct 90 - Version 1.0 by Gershon Elber.                      *
  8. ******************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include "intr_loc.h"
  14. #include "intr_gr.h"
  15.  
  16. #define PULL_DOWN_BORDER 10
  17. #define PULL_DOWN_BASE_LINE 12
  18.  
  19. static int GRLastColor = 0;               /* GR Color set before query. */
  20.  
  21. static void MenuProlog(void);
  22. static void MenuEpilog(void);
  23.  
  24. /****************************************************************************
  25. * Save current graphic state.                            *
  26. ****************************************************************************/
  27. static void MenuProlog()
  28. {
  29.     /* Save current graphic state. */
  30.     GRPushViewPort();
  31.     _GRSetViewPort(0, 0, GRScreenMaxX, GRScreenMaxY);
  32.  
  33.     GRPushTextSetting();
  34.     GRSetTextJustify(GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_VJUSTIFY_BOTTOM);
  35.     GRSetSTextStyle(GR_FONT_DEFAULT, GR_HORIZ_DIR, GR_TEXT_MAG_1);
  36.  
  37.     GRLastColor = GRGetColor();
  38. }
  39.  
  40. /****************************************************************************
  41. * Restore current graphic state.                        *
  42. ****************************************************************************/
  43. static void MenuEpilog(void)
  44. {
  45.     GRPopTextSetting();
  46.     GRSetColor(GRLastColor);
  47.  
  48.     GRPopViewPort();
  49. }
  50.  
  51. /******************************************************************************
  52. * Routine to free a pull down menu.                          *
  53. ******************************************************************************/
  54. void IntrPullDownMenuDelete(IntrPullDownMenuStruct *PDMenu)
  55. {
  56.     _IntrFree(PDMenu);
  57. }
  58.  
  59. /******************************************************************************
  60. * Routine to create a pop up menu.                          *
  61. * If SizeOfEntry = 0 then StrEntries is an array of (char *). Otherwise it    *
  62. * holds the size of each entry in StrEntries which is a long string itself.   *
  63. * NumOfEntries defines the length of the menu.                      *
  64. * It should be noted that in both cases StrEntries is NOT copied.          *
  65. * ActionFuncs holds pointer to the action functions to be called upon menu    *
  66. * selection. Functions are given no parameter and should return none.          *
  67. * It should be noted that ActionFuncs array is not copied.              *
  68. ******************************************************************************/
  69. IntrPullDownMenuStruct *IntrPullDownMenuCreate(char **StrEntries,
  70.                            int SizeOfEntry,
  71.                                                int NumOfEntries,
  72.                            IntrIntFunc *ActionFuncs,
  73.                                IntrColorType FrameColor,
  74.                                IntrColorType BackColor,
  75.                                IntrColorType ForeColor,
  76.                                IntrColorType XorColor,
  77.                                    int FrameWidth)
  78. {
  79.     IntrPullDownMenuStruct *PDMenu;
  80.  
  81.     PDMenu = (IntrPullDownMenuStruct *)
  82.                 _IntrMalloc(sizeof(IntrPullDownMenuStruct));
  83.     PDMenu -> ForeColor = ForeColor;
  84.     PDMenu -> BackColor = BackColor;
  85.     PDMenu -> FrameColor = FrameColor;
  86.     PDMenu -> XorColor = XorColor;
  87.     PDMenu -> FrameWidth = FrameWidth;
  88.     PDMenu -> NumOfEntries = NumOfEntries;
  89.     PDMenu -> SizeOfEntry = SizeOfEntry;
  90.     PDMenu -> StrEntries = StrEntries;
  91.     PDMenu -> ActionFuncs = ActionFuncs;
  92.     PDMenu -> _ActiveIndex = -1;
  93.  
  94.     return PDMenu;
  95. }
  96.  
  97. /****************************************************************************
  98. * Routine to change pop up menu entry i. Only menus defined using array of  *
  99. * strings can be modified in this way (PDMenu -> SizeOfEntry must be zero). *
  100. ****************************************************************************/
  101. void IntrPullDownSetEntry(IntrPullDownMenuStruct *PDMenu, char *Entry,
  102.                                 int Index)
  103. {
  104.     if (PDMenu -> SizeOfEntry == 0)
  105.         PDMenu -> StrEntries[Index] = Entry;
  106.     else
  107.         strncpy((char *) &PDMenu -> StrEntries[Index * PDMenu -> SizeOfEntry],
  108.             Entry,
  109.         PDMenu -> SizeOfEntry - 1);
  110. }
  111.  
  112. /****************************************************************************
  113. * Routine to change pop up menu entry i. Only menus defined using array of  *
  114. * strings can be modified in this way (PDMenu -> SizeOfEntry must be zero). *
  115. ****************************************************************************/
  116. void IntrPullDownSetAction(IntrPullDownMenuStruct *PDMenu,
  117.                 IntrIntFunc ActionFunc, int Index)
  118. {
  119.     PDMenu -> ActionFuncs[Index] = ActionFunc;
  120. }
  121.  
  122. /****************************************************************************
  123. * Routine to change pop up menu frame width.                    *
  124. ****************************************************************************/
  125. void IntrPullDownSetFrameWidth(IntrPullDownMenuStruct *PDMenu, int FrameWidth)
  126. {
  127.     PDMenu -> FrameWidth = FrameWidth;
  128. }
  129.  
  130. /****************************************************************************
  131. * Routine to change pop up menu colors.                        *
  132. ****************************************************************************/
  133. void IntrPullDownSetColors(IntrPullDownMenuStruct *PDMenu,
  134.                    IntrColorType FrameColor,
  135.                    IntrColorType BackColor,
  136.                    IntrColorType ForeColor,
  137.                    IntrColorType XorColor)
  138. {
  139.     PDMenu -> ForeColor = ForeColor;
  140.     PDMenu -> BackColor = BackColor;
  141.     PDMenu -> FrameColor = FrameColor;
  142.     PDMenu -> XorColor = XorColor;
  143. }
  144.  
  145. /****************************************************************************
  146. * Routine to draw the given pull down menu items at the given position.        *
  147. * The window is assumed to be fully visible.                    *
  148. ****************************************************************************/
  149. void _IntrPullDownMenuDrawItems(IntrPullDownMenuStruct *PDMenu)
  150. {
  151.     int i;
  152.     char
  153.         **Str = PDMenu -> StrEntries;
  154.     _IntrWindowStruct
  155.     *Window = _IntrFindWndwUsingID(PDMenu -> WindowID);
  156.     int Xmin = Window -> BBox.Xmin - Window -> FrameWidth,
  157.     Xmax = Window -> BBox.Xmax + Window -> FrameWidth,
  158.     Ymin = Window -> BBox.Ymin - Window -> FrameWidth - 1,
  159.         Dx = (Xmax - Xmin) / PDMenu -> NumOfEntries,
  160.         n = PDMenu -> NumOfEntries - 1;
  161.  
  162.     MenuProlog();
  163.  
  164.     for (i = 0; i <= n; i++) {
  165.     _IntrWndwPutNameHeader(Xmin, i != n ? Xmin + Dx - 1 : Xmax, Ymin,
  166.                                PDMenu -> FrameWidth,
  167.                                PDMenu -> SizeOfEntry == 0 ?
  168.                    Str[i] :
  169.                    (char *) &Str[i * PDMenu -> SizeOfEntry],
  170.                    FALSE, PDMenu -> FrameColor,
  171.                                PDMenu -> ForeColor, PDMenu -> BackColor,
  172.                                TRUE);
  173.         Xmin += Dx;
  174.     }
  175.     PDMenu -> DrawnEntryWidth = Dx;
  176.  
  177.     if (PDMenu -> _ActiveIndex >= 0)
  178.     _IntrPullDownInvertEntry(PDMenu -> _ActiveIndex, Window);
  179.  
  180.     MenuEpilog();
  181. }
  182.  
  183. /****************************************************************************
  184. * Routine to invert a given entry in a pull down menu.                *
  185. * It is assumed toe menu is whole visible.                    *
  186. ****************************************************************************/
  187. void _IntrPullDownInvertEntry(int Index,
  188.                   _IntrWindowStruct *Window)
  189. {
  190.     IntrPullDownMenuStruct
  191.         *PDMenu = Window -> PDMenu;
  192.     int Xmin = Window -> BBox.Xmin - Window -> FrameWidth,
  193.     Xmax = Window -> BBox.Xmax + Window -> FrameWidth,
  194.     Ymin = Window -> BBox.Ymin,
  195.     Ymax = Window -> BBox.Ymin - (PDMenu -> FrameWidth << 1) - 1,
  196.         Dx = (Xmax - Xmin) / PDMenu -> NumOfEntries;
  197.  
  198.  
  199.     GRPushTextSetting();
  200.     GRSetSTextStyle(GR_FONT_DEFAULT, GR_HORIZ_DIR, GR_TEXT_MAG_1);
  201.     Ymin -= IntrWndwGetHeaderHeight("M", PDMenu -> FrameWidth) + 1;
  202.     GRPopTextSetting();
  203.  
  204.     Xmin += Dx * Index;
  205.     if (Index == PDMenu -> NumOfEntries - 1)
  206.         Xmax -= PDMenu -> FrameWidth;
  207.     else
  208.         Xmax = Xmin + Dx - PDMenu -> FrameWidth - 1;
  209.     Xmin += PDMenu -> FrameWidth;
  210.  
  211.     MenuProlog();
  212.  
  213.     IntrAllocColor(PDMenu -> XorColor, INTR_INTENSITY_VHIGH);
  214.     GRXORRectangle(Xmin, Ymin, Xmax, Ymax);
  215.  
  216.     MenuEpilog();
  217. }
  218.  
  219. /****************************************************************************
  220. * Routine to attempt to match given location with one of the elements in    *
  221. * items displayed. Item are displayed from Left to Right.            *
  222. * Returns the index that matches, or -1 if No match.                *
  223. ****************************************************************************/
  224. int _IntrPullDownMatchPosition(int x, int y,
  225.                    _IntrWindowStruct *Window)
  226. {
  227.     IntrPullDownMenuStruct
  228.         *PDMenu = Window -> PDMenu;
  229.     int Xmin = Window -> BBox.Xmin,
  230.     Xmax = Window -> BBox.Xmax,
  231.     Ymin = Window -> BBox.Ymin + PDMenu -> FrameWidth -
  232.             IntrWndwGetHeaderHeight("M", PDMenu -> FrameWidth) - 1,
  233.     Ymax = Window -> BBox.Ymin - PDMenu -> FrameWidth,
  234.         Dx = (Xmax - Xmin) / PDMenu -> NumOfEntries;
  235.  
  236.     /* Fast clipping if cursor is not in the menu at all. */
  237.     if (y > Ymax || y < Ymin) return -1;
  238.  
  239.     return (x - Xmin) / Dx;
  240. }
  241.